﻿#ifndef _BASE_TYPE_H_
#define _BASE_TYPE_H_

#include <cstdint>
#include <memory>

typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;

typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;


// 定义拼接智能指针类型的宏
#define DEFINE_PTR(ClassName) using ClassName##Ptr = std::shared_ptr<ClassName>; \
using ClassName##UPtr = std::unique_ptr<ClassName>; \
using ClassName##WPtr = std::weak_ptr<ClassName>;

#ifdef WIN32
#define FORCE_INLINE __forceinline
#else
#define FORCE_INLINE inline
#endif
namespace as
{
	FORCE_INLINE void swapfloat(float *p)
	{
		union { float asfloat; uint8 asbytes[4]; } u1, u2;
		u1.asfloat = *p;
		/* swap! */
		u2.asbytes[0] = u1.asbytes[3];
		u2.asbytes[1] = u1.asbytes[2];
		u2.asbytes[2] = u1.asbytes[1];
		u2.asbytes[3] = u1.asbytes[0];
		*p = u2.asfloat;
	}

	FORCE_INLINE double swapdouble(double p)
	{
		union { double asfloat; uint8 asbytes[8]; } u1, u2;
		u1.asfloat = p;
		/* swap! */
		u2.asbytes[0] = u1.asbytes[7];
		u2.asbytes[1] = u1.asbytes[6];
		u2.asbytes[2] = u1.asbytes[5];
		u2.asbytes[3] = u1.asbytes[4];
		u2.asbytes[4] = u1.asbytes[3];
		u2.asbytes[5] = u1.asbytes[2];
		u2.asbytes[6] = u1.asbytes[1];
		u2.asbytes[7] = u1.asbytes[0];

		return u2.asfloat;
	}

	FORCE_INLINE void swapdouble(double *p)
	{
		union { double asfloat; uint8 asbytes[8]; } u1, u2;
		u1.asfloat = *p;
		/* swap! */
		u2.asbytes[0] = u1.asbytes[7];
		u2.asbytes[1] = u1.asbytes[6];
		u2.asbytes[2] = u1.asbytes[5];
		u2.asbytes[3] = u1.asbytes[4];
		u2.asbytes[4] = u1.asbytes[3];
		u2.asbytes[5] = u1.asbytes[2];
		u2.asbytes[6] = u1.asbytes[1];
		u2.asbytes[7] = u1.asbytes[0];
		*p = u2.asfloat;
	}

	FORCE_INLINE float swapfloat(float p)
	{
		union { float asfloat; uint8 asbytes[4]; } u1, u2;
		u1.asfloat = p;
		/* swap! */
		u2.asbytes[0] = u1.asbytes[3];
		u2.asbytes[1] = u1.asbytes[2];
		u2.asbytes[2] = u1.asbytes[1];
		u2.asbytes[3] = u1.asbytes[0];

		return u2.asfloat;
	}

	FORCE_INLINE uint16 swap16(uint16 p)
	{
		return ((p >> 8) & 0xff) | (p << 8);
	}

	FORCE_INLINE int32 swap32(int32 p)
	{
		return ((p >> 24) & 0xff) | ((p >> 8) & 0xff00) | ((p << 8) & 0xff0000) | (p << 24);
	}

	FORCE_INLINE int64 swap64(int64 p)
	{
		return ((((p >> 56) & 0xff)) | ((p >> 40) & 0x000000000000ff00ULL) | ((p >> 24) & 0x0000000000ff0000ULL) | ((p >> 8) & 0x00000000ff000000ULL) |
			((p << 8) & 0x000000ff00000000ULL) | ((p << 24) & 0x0000ff0000000000ULL) | ((p << 40) & 0x00ff000000000000ULL) | ((p << 56)));
	}

	FORCE_INLINE void swap32(uint32* p)
	{
		*p = ((*p >> 24 & 0xff)) | ((*p >> 8) & 0xff00) | ((*p << 8) & 0xff0000) | (*p << 24);
	}

	FORCE_INLINE void swap64(int64* p)
	{
		*p = ((*p >> 56) & 0xff) | ((*p >> 40) & 0x000000000000ff00ULL) | ((*p >> 24) & 0x0000000000ff0000ULL) | ((*p >> 8) & 0x00000000ff000000ULL) |
			((*p << 8) & 0x000000ff00000000ULL) | ((*p << 24) & 0x0000ff0000000000ULL) | ((*p << 40) & 0x00ff000000000000ULL) | ((*p << 56));
	}

	FORCE_INLINE int16 swap16(int16 p)
	{
		return ((p >> 8) & 0xff) | (p << 8);
	}

	FORCE_INLINE void swap64(uint64* p)
	{
		*p = ((*p >> 56)) | ((*p >> 40) & 0x000000000000ff00ULL) | ((*p >> 24) & 0x0000000000ff0000ULL) | ((*p >> 8) & 0x00000000ff000000ULL) |
			((*p << 8) & 0x000000ff00000000ULL) | ((*p << 24) & 0x0000ff0000000000ULL) | ((*p << 40) & 0x00ff000000000000ULL) | ((*p << 56));
	}

	FORCE_INLINE void swap32(int32* p)
	{
		*p = ((*p >> 24) & 0xff) | ((*p >> 8) & 0xff00) | ((*p << 8) & 0xff0000) | (*p << 24);
	}

	FORCE_INLINE uint32 swap32(uint32 p)
	{
		return ((p >> 24) & 0xff) | ((p >> 8) & 0xff00) | ((p << 8) & 0xff0000) | (p << 24);
	}

	FORCE_INLINE uint64 swap64(uint64 p)
	{
		p = (((p >> 56) & 0xff)) | ((p >> 40) & 0x000000000000ff00ULL) | ((p >> 24) & 0x0000000000ff0000ULL) | ((p >> 8) & 0x00000000ff000000ULL) |
			((p << 8) & 0x000000ff00000000ULL) | ((p << 24) & 0x0000ff0000000000ULL) | ((p << 40) & 0x00ff000000000000ULL) | ((p << 56)); return p;
	}

	FORCE_INLINE void swap16(uint16* p)
	{
		*p = ((*p >> 8) & 0xff) | (*p << 8);
	}

	FORCE_INLINE void swap16(int16* p)
	{
		*p = ((*p >> 8) & 0xff) | (*p << 8);
	}
}

#endif